﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class Palyer : MonoBehaviour
{
    
    public float smoothing = 8;
    // 休息时间
    public float restTime = 0.25f;
    private float restTimer = 0; // 计算累计时间的
    // Start is called before the first frame update
    public Vector2 targetPos = new Vector2(1, 1);
    private Rigidbody2D _rigibody;
    private BoxCollider2D _collider;
    Animator _animator;
    void Start()
    {
        _rigibody = GetComponent<Rigidbody2D>();
        _collider = GetComponent<BoxCollider2D>();
        _animator = GetComponent<Animator>();

        // 掉血定时器
        // InvokeRepeating("setHP", 0.25f, 0.25f);
    }

    private void setHP() {
        Director.Singles.HP--;
    }

    private void PlayerAttacked() {
        _animator.SetBool("Attacked", true);
        Director.Singles.HP -= 20;
    }
    // Update is called once per frame
    void Update()
    {
        if (Director.Singles.HP <= 0) {
            return;
        }
        _rigibody.MovePosition(Vector2.Lerp(transform.position, targetPos, smoothing * restTimer));

        restTimer += Time.deltaTime;
        
        if (restTimer < restTime) return;
        float h = Input.GetAxisRaw("Horizontal");// 左右
        float v = Input.GetAxisRaw("Vertical");// 上下

        if (h != 0) {
            _rigibody.constraints -= RigidbodyConstraints2D.FreezeRotation;
            _rigibody.transform.rotation = Quaternion.Euler(0, 90 - h * 90, 0);
            _rigibody.constraints = RigidbodyConstraints2D.FreezeRotation;
            v = 0;
        } 

        if (h != 0 || v != 0) {
            // 检测
            _collider.enabled = false;
            RaycastHit2D hit = Physics2D.Linecast(targetPos, targetPos + new Vector2(h, v));
            _collider.enabled = true;            
            if (hit.transform == null) {
                Director.Singles.HP -= 1;
                targetPos += new Vector2(h, v);
                Director.Singles.sendMessToEnemy("move");
                
            } else {
                switch (hit.collider.tag) {
                    case "OutWall": 
                        break;
                    case "Obstacle":
                        _animator.SetBool("Attack", true);
                        hit.collider.SendMessage("attacked");
                        break;
                    case "Food":
                        Director.Singles.HP += 10;
                        targetPos += new Vector2(h, v);
                        Director.Singles.sendMessToEnemy("move");
                        Destroy(hit.transform.gameObject);
                        break;
                    case "Roda":
                        Director.Singles.HP += 20;
                        targetPos += new Vector2(h, v);
                        Director.Singles.sendMessToEnemy("move");
                        Destroy(hit.transform.gameObject);
                        break;
                    case "Enemy":
                        break;
                    case "Exit":
                        targetPos += new Vector2(h, v);
                        // Debug.Log("胜利");
                        SceneManager.LoadScene(SceneManager.GetActiveScene().name);
                        break;
                }
            }
            restTimer = 0;
        } 
    }
}
